import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;

public class Main {

    // 明文
    private static final byte[] PLAIN_BYTES = {
            0x32, 0x43, (byte) 0xf6, (byte) 0xa8,
            (byte)0x88, 0x5a, 0x30, (byte) 0x8d,
            0x31, 0x31, (byte) 0x98, (byte) 0xa2,
            (byte) 0xe0, 0x37, 0x07, 0x34
    };

    // 密钥
    private static final byte[] KEY_BYTES = {
            0x2b, 0x7e, 0x15, 0x16,
            0x28, (byte) 0xae, (byte) 0xd2, (byte) 0xa6,
            (byte) 0xab, (byte) 0xf7, 0x15, (byte) 0x88,
            0x09, (byte) 0xcf, 0x4f, 0x3c
    };

    // 字节数组转十六进制字符串
    private static String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x ", b));
        }
        return sb.toString().trim();
    }

    public static void main(String[] args) throws Exception {
        // 创建AES密钥
        SecretKeySpec key = new SecretKeySpec(KEY_BYTES, "AES");

        // 初始化加密器
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

        // 加密
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(PLAIN_BYTES);

        System.out.println("原始明文: \n" + bytesToHex(PLAIN_BYTES));
        System.out.println("加密结果: \n" + bytesToHex(encrypted));

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decrypted = cipher.doFinal(encrypted);

        System.out.println("解密结果: \n" + bytesToHex(decrypted));

        // 验证解密结果
        System.out.println("解密验证: " +
                (Arrays.equals(PLAIN_BYTES, decrypted) ? "成功" : "失败"));
    }
}